package org.jash.shop2212;

import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.bumptech.glide.Glide;
import com.google.android.material.appbar.MaterialToolbar;

import java.util.HashMap;

public class DetailActivity extends AppCompatActivity {
    private TextView title, price, goodsNo;
    private ImageView icon, imgOne, imgTwo;
    private GoodsEntity.DataDTO data;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);
        title = findViewById(R.id.title);
        price = findViewById(R.id.price);
        goodsNo = findViewById(R.id.goods_no);
        icon = findViewById(R.id.icon);
        imgOne = findViewById(R.id.img_one);
        imgTwo = findViewById(R.id.img_two);
        data = (GoodsEntity.DataDTO) getIntent().getSerializableExtra("a");
        title.setText(data.getGoodsDesc());
        price.setText(data.getGoodsPrice() + "元");
        goodsNo.setText("商品编号：" + data.getId());
        Glide.with(icon)
                .load(data.getGoodsIcon())
                .into(icon);
        Glide.with(imgOne)
                .load(data.getGoodsDetailOne())
                .into(imgOne);
        Glide.with(imgTwo)
                .load(data.getGoodsDetailTwo())
                .into(imgTwo);
        findViewById(R.id.add_to_cart).setOnClickListener(v -> {
            HttpUtils.doGet("http://10.161.9.80:7003/car/addCar", new HashMap<String, Object>() {{
                        // 写入参数
                put("goods_id", data.getId());
                put("username", "112");
                    }}, AddCartEntity.class,
                    r -> {
                        // 正确的处理
                        Toast.makeText(this, r.getErrorMessage(), Toast.LENGTH_SHORT).show();
                    }, t -> {
                        // 错误的处理
                        Toast.makeText(this, t.getMessage(), Toast.LENGTH_SHORT).show();
                    });
        });
        findViewById(R.id.back).setOnClickListener(v -> {
            finish();
        });
        ((MaterialToolbar) findViewById(R.id.toolbar)).setNavigationOnClickListener(v -> {
            finish();
        });
    }
}